package com.example.rent.controller.tly;


import com.alibaba.fastjson.JSON;
import com.example.rent.entity.Meeting;
import com.example.rent.entity.Tenant;
import com.example.rent.entity.Teninhouse;
import com.example.rent.mapper.TeninhouseMapper;
import com.example.rent.mapper.tly.TenantMapper;
import com.example.rent.service.HouseService;
import com.example.rent.service.MeetingService;
import com.example.rent.service.TenantService;
import com.example.rent.service.TeninhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanted
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    TenantService tenantService;
    @Autowired
    HouseService houseService;
    @Autowired
    TeninhouseService teninhouseService;
    @Autowired
    TenantMapper tenantMapper;
    @Autowired
    TeninhouseMapper teninhouseMapper;
    @Autowired
    MeetingService meetingService;

    //登录
    @RequestMapping("/login")
    public String getInfo(@RequestParam("username")String username,
                          @RequestParam("password")String password){
        String signal=null;
        Tenant tenant=null;
        try {
            tenant= tenantService.getById(username);
            System.out.println(tenant);
            if (tenant.getPassword().equals(password)){
                signal = tenant.getTename();
            }else {
                signal = "error";
            }
        }
        catch (Exception e){
            signal = "error";
        }
        System.out.println(tenant);
        return signal;
    }

    //获取房源信息
    @RequestMapping("/getAllHouse")
    public List getAllHouse(){
        return houseService.list();
    }

    class Newteninhouse{
        String tphone;
        String houseaddress;
        LocalDateTime livetime;

        public void setTphone(String tphone) {
            this.tphone = tphone;
        }

        public void setHouseaddress(String houseaddress) {
            this.houseaddress = houseaddress;
        }

        public void setLivetime(LocalDateTime livetime) {
            this.livetime = livetime;
        }

        public LocalDateTime getLivetime() {
            return livetime;
        }

        public String getHouseaddress() {
            return houseaddress;
        }

        public String getTphone() {
            return tphone;
        }
    }


    //注册
    @RequestMapping("/tenantRegister")
    public String tenantRegister(HttpServletRequest request){
        Tenant tenant = JSON.parseObject(request.getParameter("tenant"),Tenant.class);
        try{
            Tenant byId = tenantService.getById(tenant.getTphone());
            if (byId.getTphone().equals(tenant.getTphone())){
                return "exit";
            }
        }catch (Exception e){
        };
        try {
            tenantService.save(tenant);
        }catch (Exception e){
            return "fail";
        }
        return "success";
    }


    //我的租赁
    @RequestMapping("/getLease")
    public List getLease(String id){
        List<Newteninhouse> list = new ArrayList<>();
        for (Teninhouse teninhouse : tenantMapper.getlist(id)) {
            Newteninhouse newteninhouse = new Newteninhouse();
            newteninhouse.setTphone(teninhouse.getTphone());
            newteninhouse.setLivetime(teninhouse.getLivetime());
            newteninhouse.setHouseaddress(tenantMapper.gethouseaddress(teninhouse.getHouseid()));
            list.add(newteninhouse);
        }
        return list;
    }



    //获取个人信息
    @RequestMapping("/getPersonalInfo")
    public Object getPersonalInfo(String id){
        return tenantService.getById(id);
    }

    //手机修改绑定
    @RequestMapping("/updatephone")
    public String updatephone(@RequestParam("id") String id,
                              @RequestParam("phone") String phone){

        try{
            Tenant tenantcount = tenantService.getById(phone);
            if(tenantcount.getTphone() != null){
                return "exit";
            }
        }catch (Exception e){
        }
        int update=0;
        try {
            tenantMapper.updatephone(id, phone);
            update=teninhouseMapper.updatephone(id, phone);

        }catch (Exception exception){
            update=0;
        }
        if(update>0){
            return "success";
        }
        return "error";
    }

    //密码修改
    @RequestMapping("/updatepwd")
    public String updatepwd(@RequestParam("id") String id,
                            @RequestParam("newpwd") String newpwd){
        int update=0;
        try{
            update = tenantMapper.updatepwd(id,newpwd);
        }catch (Exception e){
        }
        if(update>0){
            return "success";
        }
        return "error";
    }

    //获取房间
    @RequestMapping("/getroom")
    public List<String> getroom(){
        System.out.println(tenantMapper.queryaddress());
        return tenantMapper.queryaddress();
    }

    //提交修改信息
    @RequestMapping("/repaire")
    public String repaire(@RequestParam("id")String id,
                          @RequestParam("roomname")String roomname,
                          @RequestParam("descript") String descript){
        Long houseid = tenantMapper.queryAddress(roomname);
        tenantMapper.insertrepaire(id,houseid,descript);
        return "success";
    }

    //改变日期类型
    public Date changeDateType(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(date);
    }
    @RequestMapping("/getDate")
    public List<String> getDate(String date) throws ParseException {
        String freetime = tenantMapper.getFreetime(Long.parseLong(date));
        String[] strings = freetime.split("至");

        List<String> list = new ArrayList<>();
        list.add(strings[0]);
        list.add(strings[1]);
        System.out.println(strings[0]+" "+strings[1]);
        return list;
    }

    //会面信息
    @RequestMapping("/remeeting")
    public String remeeting(HttpServletRequest request){
        Meeting meeting = JSON.parseObject(request.getParameter("meeting"), Meeting.class);
        String info = "";
        String gethouseaddress = tenantMapper.gethouseaddress(meeting.getHouseid());
        meeting.setHouseaddress(gethouseaddress);
        try {
            meetingService.save(meeting);
        }catch (Exception e){
            return "exit";
        }

        return "success";
    }
}

